package com.hrm.attendance.service;

import com.hrm.attendance.dao.AttendanceDao;
import com.hrm.attendance.dao.CompanySettingsDao;
import com.hrm.attendance.dao.UserDao;
import com.hrm.common.entity.PageResult;
import com.hrm.common.util.DateUtil;
import com.hrm.common.util.IdWorker;
import com.hrm.domain.attendance.bo.AtteItemBO;
import com.hrm.domain.attendance.entity.ArchiveMonthlyInfo;
import com.hrm.domain.attendance.entity.Attendance;
import com.hrm.domain.attendance.entity.CompanySettings;
import com.hrm.domain.system.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AtteService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private CompanySettingsDao companySettingsDao;
    @Autowired
    private AttendanceDao attendanceDao;

    public Map<String, Object> getAtteData(String companyId, int page, int pageSize) throws ParseException {
        //考勤月
        CompanySettings settings = companySettingsDao.findById(companyId).get();
        String dataMonth = settings.getDataMonth(); //考勤日期201907
        //分页查询用户
        Page<User> userPage = userDao.findPage(companyId, PageRequest.of(page - 1, pageSize));
        List<AtteItemBO> list = new ArrayList<>();
        //获取每个用户的考勤
        for (User user : userPage.getContent()) {
            AtteItemBO bo = new AtteItemBO();
            BeanUtils.copyProperties(user, bo); //spring提供的方法类，将user的属性值copy到bo的同名属性中
            List<Attendance> attendanceRecord = new ArrayList<>();
            //获取当前月的所有天数
            String[] days = DateUtil.getDaysByYearMonth(dataMonth);
            //循环每天获取考勤记录
            for (String day : days) {
                Attendance byUserIdAndDay = attendanceDao.findByUserIdAndDay(user.getId(), day);
                if (byUserIdAndDay == null) {
                    //即该员工当天无考勤记录
                    byUserIdAndDay = new Attendance();
                    byUserIdAndDay.setAdtStatu(2);
                    byUserIdAndDay.setId(new IdWorker().nextId() + "");
                    byUserIdAndDay.setUserId(user.getId());
                    byUserIdAndDay.setDay(day);
                }
                attendanceRecord.add(byUserIdAndDay);
            }
            bo.setAttendanceRecord(attendanceRecord);
            list.add(bo);
        }
        //返回值构造：map：分页对象，待处理的考勤数量、考勤月份
        Map<String,Object> map = new HashMap<>();
        PageResult<AtteItemBO> result = new PageResult<>(userPage.getTotalElements(), list);
        map.put("data",result);
        map.put("tobeTaskCount",0);
        map.put("monthOfReport",Integer.parseInt(dataMonth.substring(4)));
        return map;
    }

    public List<ArchiveMonthlyInfo> getReport(String atteDate,String companyId) {
        //查询企业内所有用户
        List<User> users = userDao.findByCompanyId(companyId);
        //循环遍历用户列表，统计每个用户当月的考勤记录
        List<ArchiveMonthlyInfo> list = new ArrayList<>();
        for (User user : users) {
            ArchiveMonthlyInfo info = new ArchiveMonthlyInfo(user);
            //统计每个用户的考勤记录(出勤多少天、矿工多少天、、、)
            Map<String, Object> map = attendanceDao.countNum(user.getId(), atteDate + "%");
            info.setStatisData(map);
            list.add(info);
        }
        return list;
    }

    public void newReport(String yearMonth, String companyId) {
        CompanySettings companySettings = companySettingsDao.findById(companyId).orElse(null);
        if (companySettings != null){
            companySettings.setDataMonth(yearMonth);
            companySettingsDao.save(companySettings);
        }
    }
}
